package com.example.demo;

import java.time.Instant;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.pojo.CommonJSONResult;
import com.example.tasks.AsyncTask;

@RestController
@RequestMapping("/dotask")
public class DoTaskController {
	@Autowired
	private AsyncTask asyncTask;
	
	@GetMapping("/test1")
	public CommonJSONResult test1() throws Exception {
		long start = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)).toEpochMilli();
		
		Future<Boolean> doTask1 = asyncTask.doTask1();
		Future<Boolean> doTask2 = asyncTask.doTask2();
		Future<Boolean> doTask3 = asyncTask.doTask3();
		
		final boolean hasDone = doTask1.isDone() && doTask2.isDone() && doTask3.isDone();
		while(!hasDone) {
			if (doTask1.isDone() && doTask2.isDone() && doTask3.isDone()) {
				break;
			}
		}
		
		long end = Instant.now().plusMillis(TimeUnit.HOURS.toMillis(8)).toEpochMilli();
		String times = "任务全部完成，总耗时："  + (end - start) + "毫秒";
		System.out.println(times);
		return CommonJSONResult.ok(times);
	}

}
